package com.ruoyi.produce.process.datacollection.service;

import com.ruoyi.produce.process.datacollection.domain.DataCollectionCond;
import com.ruoyi.produce.process.datacollection.domain.DataCollection;
import com.ruoyi.produce.process.datacollection.domain.RealTimeCapacity;
import com.ruoyi.produce.process.datacollection.mapper.DataCollectionMapper;
import com.ruoyi.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产数据
 * 业务处理层
 */
@Service
public class DataCollectionServiceImpl implements IDataCollectionService{
    @Autowired
    private DataCollectionMapper dataCollectionMapper;

//    @Autowired
//    private WebSocket webSocket;

    /**
     * 根据搜索条件所有生产数据记录
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<DataCollection> selectDataCollectionList(DataCollectionCond cond) {
        return dataCollectionMapper.selectDataCollectionList(cond);
    }

    /**
     * 根据编号搜索具体的生产数据记录
     * @return
     */
    @Override
    public DataCollection selectDataCollectionById(String shiftInfoId, String machineId, String workSheetId) {
        return dataCollectionMapper.selectDataCollectionById(shiftInfoId, machineId, workSheetId);
    }

    /**
     * 新增生产数据记录
     * @param dataCollection
     * @return
     */
    @Override
    public int insertDataCollection(DataCollection dataCollection) {
//        System.out.println("对象" + dataCollection);
        return dataCollectionMapper.insertDataCollection(dataCollection);
    }

    /**
     * 修改生产数据
     * @param dataCollection
     * @return
     */
    @Override
    public int updateDataCollection(DataCollection dataCollection) {
        return dataCollectionMapper.updateDataCollection(dataCollection);
    }

    /**
     * 删除生产数据记录
     * 仅修改删除标志
     * @param dataCollection
     * @return
     */
    @Override
    public int updateDataCollectionDeleteFlag(DataCollection dataCollection) {
        return dataCollectionMapper.updateDataCollectionDeleteFlag(dataCollection);
    }

    /**
     * 查询实时产能
     * @param cond
     * @return
     */
    @Override
    public List<RealTimeCapacity> searchRealTimeCapacity(DataCollectionCond cond) {
        List<RealTimeCapacity> realTimeCapacityList = new ArrayList<>(); // 返回类
        List<DataCollection> dataCollectionList = dataCollectionMapper.selectDataCollectionList(cond);
        for (DataCollection item : dataCollectionList) {
            if (item.getCreateTime() != null && item.getUpdateTime() != null) {
                long time = TimeUtil.TimeSubtractionSecond(item.getUpdateTime(), item.getCreateTime());
                int capacity = (int) (time / (item.getProduceNumber() + 1));
                long minute = TimeUtil.TimeSubtractionMinute(item.getUpdateTime(), item.getCreateTime());
                RealTimeCapacity realTimeCapacity = new RealTimeCapacity(item.getMachineId(), item.getMachineName(), item.getProduceId(), item.getProduceName(), capacity, minute);
                realTimeCapacityList.add(realTimeCapacity);
            }
        }
        return realTimeCapacityList;
    }
}
